package com.have.blog.admin.redis.impl;

import com.have.blog.admin.constant.AdminRedisConstant;
import com.have.blog.admin.redis.AdminBlogCacheService;
import com.have.blog.base.constant.BaseRedisConstant;
import com.have.blog.config.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class AdminBlogCacheServiceImpl implements AdminBlogCacheService {

    @Value("${redis.admin.database}")
    private String REDIS_KEY_DATABASE;
    @Autowired
    private RedisService redisService;

    @Override
    public List<Map<String, Object>> getBlogCountByTag() {
        String key = REDIS_KEY_DATABASE + AdminRedisConstant.SEGMENTATION +
                BaseRedisConstant.DASHBOARD + AdminRedisConstant.SEGMENTATION +
                BaseRedisConstant.BLOG_COUNT_BY_TAG;
        return (List<Map<String, Object>>) redisService.get(key);
    }

    @Override
    public void saveBlogCountByTag(List<Map<String, Object>> data, long expiration, TimeUnit timeUnit) {
        String key = REDIS_KEY_DATABASE + AdminRedisConstant.SEGMENTATION +
                BaseRedisConstant.DASHBOARD + AdminRedisConstant.SEGMENTATION +
                BaseRedisConstant.BLOG_COUNT_BY_TAG;
        redisService.set(key, data, expiration, timeUnit);
    }

    @Override
    public void delBlogCountByTag() {
        String key = REDIS_KEY_DATABASE + AdminRedisConstant.SEGMENTATION +
                BaseRedisConstant.DASHBOARD + AdminRedisConstant.SEGMENTATION +
                BaseRedisConstant.BLOG_COUNT_BY_TAG;
        redisService.del(key);
    }



    @Override
    public List<Map<String, Object>> getBlogCountByCategory() {
        String key = REDIS_KEY_DATABASE + AdminRedisConstant.SEGMENTATION +
                BaseRedisConstant.DASHBOARD + AdminRedisConstant.SEGMENTATION +
                BaseRedisConstant.BLOG_COUNT_BY_CATEGORY;
        return (List<Map<String, Object>>) redisService.get(key);
    }

    @Override
    public void saveBlogCountByCategory(List<Map<String, Object>> data, long expiration, TimeUnit timeUnit) {
        String key = REDIS_KEY_DATABASE + AdminRedisConstant.SEGMENTATION +
                BaseRedisConstant.DASHBOARD + AdminRedisConstant.SEGMENTATION +
                BaseRedisConstant.BLOG_COUNT_BY_CATEGORY;
        redisService.set(key, data, expiration, timeUnit);
    }

    @Override
    public void delBlogCountByCategory() {
        String key = REDIS_KEY_DATABASE + AdminRedisConstant.SEGMENTATION +
                BaseRedisConstant.DASHBOARD + AdminRedisConstant.SEGMENTATION +
                BaseRedisConstant.BLOG_COUNT_BY_CATEGORY;
        redisService.del(key);
    }

    /**
     * 博客贡献数
     * @return
     */
    @Override
    public Map<String, Object> getBlogContributeCount() {
        String key = REDIS_KEY_DATABASE + AdminRedisConstant.SEGMENTATION +
                AdminRedisConstant.SEGMENTATION + BaseRedisConstant.BLOG_CONTRIBUTE_COUNT;
        return (Map<String, Object>) redisService.get(key);
    }

    @Override
    public void saveBlogContributeCount(Map<String, Object> data, long expiration, TimeUnit timeUnit) {
        String key = REDIS_KEY_DATABASE + AdminRedisConstant.SEGMENTATION +
                AdminRedisConstant.SEGMENTATION + BaseRedisConstant.BLOG_CONTRIBUTE_COUNT;
        redisService.set(key, data, expiration, timeUnit);
    }

    @Override
    public void delBlogContributeCount() {
        String key = REDIS_KEY_DATABASE + AdminRedisConstant.SEGMENTATION +
                AdminRedisConstant.SEGMENTATION + BaseRedisConstant.BLOG_CONTRIBUTE_COUNT;
        redisService.del(key);
    }
}
